//
//  HYHeadRefreshView.m
//  HYRefresh
//
//  Created by HYMac on 16/3/4.
//  Copyright © 2016年 HYMac. All rights reserved.
//

#import "HYHeadRefreshView.h"

#import "HYCustomTextView.h"
@interface HYHeadRefreshView()
//@property(nonatomic, weak)UILabel *stateLabel;
@property(nonatomic, weak)UIView *contentV;
@property(nonatomic, weak)UIImageView *arrowView;
@property(nonatomic, weak)HYCustomTextView *stateView;
@end
@implementation HYHeadRefreshView
-(UIView *)contentV
{
    if (_contentV==nil)
    {
        UIView *contentV=[[UIView alloc]initWithFrame:CGRectMake(0, _scrollView.contentSize.height, IOS_screen.width, 50)];
        //contentV.backgroundColor=[UIColor yellowColor];
        [_scrollView addSubview:contentV];
        _contentV=contentV;
    }
    
    return _contentV;
}
-(HYCustomTextView *)stateView
{
    if (_stateView==nil)
    {
        CGFloat labelW=self.contentV.frame.size.width*0.4;
        CGFloat labelH=self.contentV.frame.size.height*0.8;
        HYCustomTextView *stateView=[[HYCustomTextView alloc]initWithFrame:CGRectMake(IOS_screen.width*0.45, self.contentV.frame.size.height*0.1, labelW, labelH)];
       
        stateView.backgroundColor=[UIColor whiteColor];
         //stateView.backgroundColor=[UIColor yellowColor];
        // label.backgroundColor=[UIColor blackColor];
        [self.contentV addSubview:stateView];
        _stateView=stateView;
    }
    return _stateView;
}
//-(UILabel *)stateLabel
//{
//    if (_stateLabel==nil)
//    {
//        CGFloat labelW=self.contentV.frame.size.width*0.4;
//        CGFloat labelH=self.contentV.frame.size.height*0.8;
//        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(IOS_screen.width*0.45, self.contentV.frame.size.height*0.1, labelW, labelH)];
//       // label.backgroundColor=[UIColor yellowColor];
//       // label.backgroundColor=[UIColor blackColor];
//        [self.contentV addSubview:label];
//        _stateLabel=label;
//    }
//    return _stateLabel;
//}
//
-(UIImageView *)arrowView
{
    if (_arrowView==nil)
    {
        CGFloat imageW=20;
        CGFloat imageH=40;
        UIImageView *imageV=[[UIImageView alloc]initWithFrame:CGRectMake(IOS_screen.width*0.35, self.contentV.frame.size.height*0.1, imageW, imageH)];
        
        _arrowView=imageV;
      //  imageV.backgroundColor=[UIColor blackColor];
        [self.contentV addSubview:imageV];
    }
    
    return _arrowView;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame])
    {
        
    }
    return self;
}
-(void)setScrollView:(UIScrollView *)scrollView
{
    _scrollView=scrollView;
   [scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
   
}
-(void)setState:(refreshState)state
{
    _state=state;
    if (state==refreshStateNoneNormal)
    {
        if (self.contentV)
        {
            [self.contentV removeFromSuperview];
        }
        
    }
    else if (state==refreshStateLowNormal)
    {
        //self.stateLabel.text=refreshStateLowNormalText;
        self.stateView.HYtext=refreshStateLowNormalText;
       
        self.arrowView.image=[UIImage imageNamed:@"arrow@2x"];
        self.arrowView.transform=CGAffineTransformIdentity;
    }
    else if (state==refreshStateHeightNormal)
    {
        
      //  self.stateLabel.text=refreshStateHeightNormalText;
        self.stateView.HYtext=refreshStateHeightNormalText;
        self.arrowView.image=[UIImage imageNamed:@"arrow@2x"];
        self.arrowView.transform=CGAffineTransformMakeScale(1, -1);
    }
    else if (state==refreshStatePulling)
    {
        
    }
    else
    {
        self.stateView.HYtext=refreshStateRefreshingText;
        _scrollView.contentInset=UIEdgeInsetsMake(0, 0, 50, 0);
        UIActivityIndicatorView *activity=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        activity.frame=self.arrowView.frame;
        //   activity.backgroundColor=[UIColor blueColor];
        //activity.color=[UIColor redColor];
    //    self.stateLabel.text=refreshStateRefreshingText;
        [self.arrowView removeFromSuperview];
        
        [self.contentV addSubview:activity];
        [activity startAnimating];
        [self.target performSelector:self.action withObject:nil];
    }
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if (!(self.state==refreshStateRefreshing))
    {
        
    if (_scrollView.decelerating&&(_scrollView.contentOffset.y>_scrollView.contentSize.height-IOS_screen.height+50))
    {
        self.state=refreshStatePulling;
        if (_scrollView.decelerating)
        {
            self.state=refreshStateRefreshing;
             return;
        }
      
      
    }
    if ((_scrollView.contentOffset.y>_scrollView.contentSize.height-IOS_screen.height)&&(_scrollView.contentOffset.y<_scrollView.contentSize.height-IOS_screen.height+50))
    {
        self.state=refreshStateLowNormal;
    }
   else if(_scrollView.contentOffset.y>_scrollView.contentSize.height-IOS_screen.height+50)
   {
       self.state=refreshStateHeightNormal;
   }
    }
}
-(void)dealloc
{
    [_scrollView removeObserver:self forKeyPath:@"contentOffset" context:nil];
}
@end
